class NumMatrix {
/**
1  2  3         1  3  6       0  0  0  0  
2  3  4  -----> 3  8  15----->0  1  3  6
2  4  5         5  14 26      0  3  8  15
                              0  5  14 26
8--26 ==  26-6-5+1
 */
    private int[][] sums;

    public NumMatrix(int[][] matrix) {
        if(matrix.length == 0 || matrix[0].length == 0) {
            return;
        }
        sums = new int[matrix.length + 1][matrix[0].length + 1];
        for(int i = 0;i < matrix.length;i++) {
            int sum = 0;
            for(int j = 0;j < matrix[0].length;j++) {
                sum += matrix[i][j];
                sums[i + 1][j + 1] = sums[i][j + 1] + sum;
            }
        }

    }
    
    public int sumRegion(int row1, int col1, int row2, int col2) {
        return sums[row2+1][col2+1] - sums[row1][col2+1] - sums[row2 + 1][col1] + sums[row1][col1];
    }
}

/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix obj = new NumMatrix(matrix);
 * int param_1 = obj.sumRegion(row1,col1,row2,col2);
 */